package cn.com.generaldata.os.service.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.com.generaldata.os.constant.FilterConstant;
import cn.com.generaldata.os.dao.intf.BaseDAOInterface;
import cn.com.generaldata.os.paging.Event;
import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.paging.FilterRule;
import cn.com.generaldata.os.paging.Page;
import cn.com.generaldata.os.paging.Result;
import cn.com.generaldata.os.pojo.AuthorFunc;
import cn.com.generaldata.os.pojo.Authority;
import cn.com.generaldata.os.pojo.Func;
import cn.com.generaldata.os.pojo.User;
import cn.com.generaldata.os.vo.AuthorityVO;
import cn.com.generaldata.os.vo.FuncVO;

@Component
public class AuthorityService {

	private static Logger log = LoggerFactory.getLogger(AuthorityService.class);

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<Func> funcDAO;
	
	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<Authority> authorDAO;
	
	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<AuthorFunc> authorFuncDAO;
	
	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<User> userDAO;

	public Result queryAuthority(Filter filter, Page page) {
		if (filter == null) {
			filter = new Filter();
		}

		List<Authority> authors = authorDAO.find(Authority.class, filter, page);
		List<AuthorityVO> vAuthorList = new ArrayList<AuthorityVO>();
		for (Authority author : authors) {
			AuthorityVO vAuthor = new AuthorityVO();
			try {
				BeanUtils.copyProperties(vAuthor, author);
				String funcs = "";
				Set<AuthorFunc> authorFunc = author.getAuthorFunc();
				for (AuthorFunc func : authorFunc) {
					funcs += func.getFunc().getFunc() + "  ";
				}
				vAuthor.setFuncs(funcs);
				vAuthorList.add(vAuthor);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				log.error("转换权限角色出错", e);
			}
		}
		int count = authorDAO.count(Authority.class, filter);

		if (page == null) {
			return new Result(1, 1, count, vAuthorList, new ArrayList<Event>());
		}
		return new Result(page.getPageNumber(), page.getPageSize(), count, vAuthorList, new ArrayList<Event>());
	}


	public void addAuthor(AuthorityVO vAuthor) {
		Authority author = new Authority();
		author.setId(UUID.randomUUID().toString().replaceAll("-", ""));
		author.setAuthorName(vAuthor.getAuthorName());
		authorDAO.save(author);
		String funcs = vAuthor.getFuncs();
		String[] split = funcs.split(",");
		for (String func : split) {
			AuthorFunc af = new AuthorFunc();
			af.setId(UUID.randomUUID().toString().replaceAll("-", ""));
			Func funcPo = new Func();
			funcPo.setId(func);
			af.setAuthority(author);
			af.setFunc(funcPo);
			authorFuncDAO.save(af);
		}
	}


	public void deleteAuthor(String id) {
		Authority authority = authorDAO.get(Authority.class, id);
		Filter filter = new Filter();
		filter.addRule(new FilterRule(FilterConstant.EQUAL, "authority.id", id));
		List<AuthorFunc> authorFuncs = authorFuncDAO.find(AuthorFunc.class, filter, null);
		for (AuthorFunc authorFunc : authorFuncs) {
			authorFuncDAO.delete(authorFunc);
		}
		authorDAO.delete(authority);
	}


	public void modifyAuthor(AuthorityVO vAuthor) {
		Authority authority = authorDAO.get(Authority.class, vAuthor.getId());
		Filter filter = new Filter();
		filter.addRule(new FilterRule(FilterConstant.EQUAL, "authority.id", vAuthor.getId()));
		List<AuthorFunc> authorFuncs = authorFuncDAO.find(AuthorFunc.class, filter, null);
		for (AuthorFunc authorFunc : authorFuncs) {
			authorFuncDAO.delete(authorFunc);
		}
		String funcs = vAuthor.getFuncs();
		String[] split = funcs.split(",");
		for (String func : split) {
			AuthorFunc af = new AuthorFunc();
			af.setId(UUID.randomUUID().toString().replaceAll("-", ""));
			Func funcPo = new Func();
			funcPo.setId(func);
			af.setAuthority(authority);
			af.setFunc(funcPo);
			authorFuncDAO.save(af);
		}
	}


	public List<FuncVO> getFuncsByAuthor(String aid) {
		Filter filter = new Filter();
		filter.addRule(new FilterRule(FilterConstant.EQUAL, "authority.id", aid));
		List<AuthorFunc> authorFuncs = authorFuncDAO.find(AuthorFunc.class, filter, null);
		List<FuncVO> vFuncList = new ArrayList<FuncVO>();
		for (AuthorFunc af : authorFuncs) {
			Func func = af.getFunc();
			FuncVO vFunc = new FuncVO();
			try {
				BeanUtils.copyProperties(vFunc, func);
				vFuncList.add(vFunc);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				log.error("转换权限功能出错", e);
			}
		}
		// List<Func> allFuncs = funcDAO.find(Func.class, null, null);

		// for (Func func : allFuncs) {

		// }
		return vFuncList;
	}


	public List<FuncVO> getAllFuncs() {
		List<Func> allFuncs = funcDAO.find(Func.class, null, null);
		List<FuncVO> vFuncList = new ArrayList<FuncVO>();
		for (Func func : allFuncs) {
			FuncVO vFunc = new FuncVO();
			try {
				BeanUtils.copyProperties(vFunc, func);
				vFuncList.add(vFunc);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				log.error("转换权限功能出错", e);
			}
		}
		return vFuncList;
	}


	public Authority getAuthority(String id) {
		return authorDAO.get(Authority.class, id);
	}

}
